package controller

import (
	"github.com/gin-gonic/gin"
	"restful-go/config"
	"restful-go/internal/auth"
	"restful-go/internal/utils"
)

type User struct {
	ID       uint
	Username string
	phone    string
}

func Login(c *gin.Context) {
	phone := c.PostForm("phone")
	var user User
	if err := config.Mysql.Where("phone = ?", phone).First(&user).Error; err != nil {
		utils.ErrorJSON(c, 401, "Invalid credentials")
		return
	}

	token, err := auth.GenerateToken(user.ID)
	if err != nil {
		utils.ErrorJSON(c, 401, "Invalid credentials")
		return
	}

	utils.SuccessJSON(c, gin.H{"token": token})
	return
}

func GetUser(c *gin.Context) {
	userId, _ := c.Get("user_id")
	utils.SuccessJSON(c, gin.H{"user_id": userId})
	return
}
